<?
include_once(LIBS_DIR.'Select/Select.class.php');

$select_tables = "
	sales s
	LEFT JOIN orders o ON (o.order_id = s.order_id)
	LEFT JOIN clients c ON(o.client_id = c.client_id)
	LEFT JOIN purchases p ON(s.purchase_id = p.purchase_id)
	LEFT JOIN products pr on(p.product_id = pr.product_id)
";

$select_tables1 = "
	orders o
";

$select_tables2 = "
	purchases p
	LEFT JOIN registerdomain rd ON(p.purchase_id = rd.purchase_id)
	LEFT JOIN products pr on(p.product_id = pr.product_id)
";

$select_tables3 = "
        sales s
        LEFT JOIN orders o ON (o.order_id = s.order_id)
        LEFT JOIN clients c ON(o.client_id = c.client_id)
";

$select_tables4 = "
        sales s
        LEFT JOIN orders o ON(o.order_id = s.order_id)
        LEFT JOIN clients c ON(o.client_id = c.client_id)
        LEFT JOIN resellers r ON(o.reseller_id = r.reseller_id)
        LEFT JOIN stores st ON(o.store_id = st.store_id)
        LEFT JOIN purchases p ON(s.purchase_id = p.purchase_id)
        LEFT JOIN products pr ON(p.product_id = pr.product_id)
";

$select_map2 = array(
	'reseller_id' => 'p.reseller_id',
	'client_id' => 'p.client_id',
	'purchase_id' => 'p.purchase_id',
	'product_id' => 'pr.product_id',
	'start_date' => 'p.start_date',
	'expire_date' => 'p.expire_date',
	'registerdomain_id' => 'rd.id',
	'domain' => 'rd.sld || \'.\' || rd.tld',
	'domain_flags' => 'rd.flags',
	'domain_ns1' => 'rd.ns1',
	'domain_ns2' => 'rd.ns2',
	'product_name' => 'pr.name',
	'product_type' => 'pr.type',
	'product_wholesale' => 'pr.wholesale',
	'product_retail' => 'pr.price',	
);

$select_map = array(
	'store' => 'st.title',
	'order_id' => 'o.order_id',
	'order_date' => 'substring(o.date from 1 for 10)',
	'order_date_' => 'substring(o.date from 1 for 19)',
	'order_date__' => 'substring(o.date from 1 for 7)',
	'order_date___' => 'substring(o.date from 1 for 4)',
	'order_number' => 'o.order_number',
	'order_status' => 'o.order_status',
	'company_id' => 'o.company_id',
	'order_amount_total' => 'round(o.total*o.payment_exchange, 2)',
	'order_amount_wholesale' => 'round(o.wholesale*o.payment_exchange, 2)',
	'wholesale_order' => '(o.total = o.wholesale)',
	'sum_order_amount_total' => 'round(sum(o.total*o.payment_exchange), 2)',
	'sum_order_amount_wholesale' => 'round(sum(o.wholesale*o.payment_exchange), 2)',
	'sum_order_amount_total_usd' => 'sum(o.total)',
	'sum_order_amount_wholesale_usd' => 'sum(o.wholesale)',
	'order_company_id' => 'o.company_id',
	'store_id' => 'o.store_id',
	'reseller_id' => 'o.reseller_id',
	'reseller' => 'r.name',
	'client_id' => 'o.client_id',
	'payment_method' => 'o.payment_method',
	'currency' => 'o.currency',
	'client_username' => 'c.username',
	'client_server' => 'c.server',
	'client_company_name' => 'o.client_company_name',
	'client_vat_num' => 'o.client_vat_num',
	'client_firstname' => 'o.client_firstname',
	'client_lastname' => 'o.client_lastname',
	'client_name' => 'o.client_firstname || \' \' || o.client_lastname',
	'client_email' => 'o.client_email',
	'client_address1' => 'o.client_address1',
	'client_address2' => 'o.client_address2',
	'client_city' => 'o.client_city',
	'client_state' => 'o.client_state',
	'client_zip' => 'o.client_zip',
	'client_country' => 'o.client_country',
	'client_phone' => 'o.client_phone',
	'client_fax' => 'o.client_fax',
	'client_ip' => 'o.ip',
	'client_individual' => '(o.client_company_name IS NULL OR o.client_company_name=\'\')',
	'client_business' => '(o.client_company_name IS NOT NULL AND o.client_company_name!=\'\')',
	'client_business_novat' => '(o.client_vat_num IS NULL OR o.client_vat_num=\'\')',
	'client_business_vat' => '(o.client_vat_num IS NOT NULL AND o.client_vat_num!=\'\')',
	'payment_exchange' => 'o.payment_exchange',
	'payment_currency' => 'o.payment_currency',
	'sale_id' => 's.sale_id',
	'product_id' => 's.product_id',
	'amount_total' => 'round(s.amount_total*o.payment_exchange, 2)',
	'amount_wholesale' => 'round(s.amount_wholesale*o.payment_exchange, 2)',
	'amount_referer' => 's.amount_referer',
	'sale_date' => 'substring(s.date from 1 for 10)',
	'sale_date_' => 'substring(s.date from 1 for 19)',
	'sale_date__' => 'substring(s.date from 1 for 7)',
	'sale_date___' => 'substring(s.date from 1 for 4)',
	'payed_reseller' => 's.payed_reseller',
	'payed_referer' => 's.payed_referer',
	'sale_number' => 's.order_number',
	'period' => 's.period',
	'sale_name' => 's.name',
	'sale_type' => 's.type',
	'sale_period' => 's.sale_period',
	'amount_vat' => 's.amount_vat',
	'amount_fee' => 's.amount_fee',
	'purchase_id' => 'p.purchase_id',
	'product_id' => 'pr.product_id',
	'start_date' => 'p.start_date',
	'expire_date' => 'p.expire_date',
	'registerdomain_id' => 'rd.id',
	'domain' => 'rd.sld || \'.\' || rd.tld',
	'domain_flags' => 'rd.flags',
	'domain_ns1' => 'rd.ns1',
	'domain_ns2' => 'rd.ns2',
	'rp_product_name' => 'rpr.name',
	'rp_product_type' => 'rpr.type',
	'product_name' => 'pr.name',
	'product_type' => 'pr.type',
	'product_wholesale' => 'pr.wholesale',
	'product_retail' => 'pr.price',
	'sum_amount_total' => 'round(sum(s.amount_total*o.payment_exchange),2)',
	'sum_amount_wholesale' => 'round(sum(s.amount_wholesale*o.payment_exchange),2)',
	'sum_amount_total_usd' => 'sum(s.amount_total)',
	'sum_amount_wholesale_usd' => 'sum(s.amount_wholesale)',
	'count' => 'count(*)'
);

$select_delimiters = array('order_date' => '%', 'sale_date' => '%');

$select_additional_map = array();

$select_aggregates = array('sum_amount_total', 'sum_amount_wholesale', 'sum_amount_total_usd', 'sum_amount_wholesale_usd', 'sum_order_amount_total', 'sum_order_amount_wholesale', 'sum_order_amount_total_usd', 'sum_order_amount_wholesale_usd', 'count');

?>